package com.sgb.knowledgeshare.weight;

import android.annotation.SuppressLint;
import android.content.Context;
import androidx.viewpager.widget.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import com.swgk.core.util.SharedPreferenceUtil;

/**
 * Created by fq on 2020/9/10 0010
 * Description：分享-首页viewpager定义向左滑动时用户中心展示控件
 */
public class HorizontalCanScrollViewPager extends ViewPager {

    //true  是不可滑动    false  是可滑动
    private boolean noScroll = false;

    public void setNoScroll(boolean noScroll) {
        this.noScroll = noScroll;
    }

    public HorizontalCanScrollViewPager(Context context) {
        this(context, null);
    }

    public HorizontalCanScrollViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
        if (!noScroll && v != this && v instanceof WrapViewPager) {
            int currentItem = ((WrapViewPager) v).getCurrentItem();
            String shareHomePosition = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.SHARE_HOME_POSITION, "0");
            if (currentItem == 0 && dx < 0 && Integer.parseInt(shareHomePosition) == 1) {
                return false;
            }
        }
        return super.canScroll(v, checkV, dx, x, y);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (noScroll)
            return false;
        else
            return super.onTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {
        if (noScroll)
            return false;
        else
            return super.onInterceptTouchEvent(arg0);
    }

}
